package com.bsb.biz.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bsb.base.common.config.Global;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.mapper.BsbFirmMapper;
import com.bsb.base.mapper.BsbUserExMapper;
import com.bsb.base.mapper.BsbUserMapper;
import com.bsb.base.model.BsbFirm;
import com.bsb.base.model.BsbUser;
import com.bsb.base.model.BsbUserExample;
import com.bsb.base.model.BsbUserExample.Criteria;
import com.bsb.base.model.Expert;
import com.bsb.base.model.Firm;
import com.bsb.base.model.UserFirm;

@Service
public class BsbUserService{
	
	@Resource
	private BsbUserMapper bsbUserMapper;
	@Resource
	private BsbFirmMapper bsbFirmMapper;
	
	@Resource
	private BsbUserExMapper bsbUserExMapper;
	
	public String getUserNameById(String id){
		if(StringUtils.isEmpty(id)){
			return null;
		}
		BsbUser selectByPrimaryKey = bsbUserMapper.selectByPrimaryKey(id);
		if(null != selectByPrimaryKey){
			return selectByPrimaryKey.getName();
		}
		return null;
	}
	
	public BsbFirm getFirmByUserId(String id){
		BsbFirm firm = new BsbFirm();
		if(StringUtils.isEmpty(id)){
			return firm;
		}
		BsbUser user = bsbUserMapper.selectByPrimaryKey(id);
		if(null == user){
			return firm;
		}
		Integer firmId = user.getFirmId();
		BsbFirm f = bsbFirmMapper.selectByPrimaryKey(firmId);
		if(null == f){
			return firm;
		}
		return f;
	}
	
	public BsbUser getUserByPhone(String phone){
		if(!StringUtils.isMobile(phone)){
			return null;
		}
		BsbUserExample example = new BsbUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andPhoneEqualTo(phone);
		criteria.andDelFlagEqualTo(Global.SHOW);
		List<BsbUser> users = bsbUserMapper.selectByExample(example );
		if(null != users && users.size()>0){
			return users.get(0);
		}
		return null;
	}
	
	public List<Expert> expertList(){
		BsbUserExample bsbUserExample = new BsbUserExample();
		Criteria criteria = bsbUserExample.createCriteria();
		criteria.andRoleEqualTo("4");
		criteria.andDelFlagEqualTo(Global.SHOW);
		List<BsbUser> users = bsbUserMapper.selectByExample(bsbUserExample);
		List<Expert> experts = new ArrayList<Expert>();
		for (BsbUser bsbUser : users) {
			Expert expert = new Expert(bsbUser.getPhone(),bsbUser.getName());
			experts.add(expert);
		}
		return experts;
	}
	
	public void addBsbUser(BsbUser bsbUser) {
		bsbUserMapper.insertSelective(bsbUser);
	}
	
	public void delBsbUserByUuid(String uuid) {
		bsbUserMapper.deleteByPrimaryKey(uuid);
	}
	
	public void updateBsbUser(BsbUser bsbUser) {
		bsbUserMapper.updateByPrimaryKeySelective(bsbUser);
	}
	
	public void updateBsbUserByExample(BsbUser bsbUser,BsbUserExample bsbUserExample){
	    bsbUserMapper.updateByExampleSelective(bsbUser,bsbUserExample);
	}
	
	public BsbUser getBsbUserByUuid(String uuid) {
		return bsbUserMapper.selectByPrimaryKey(uuid);
	}
	
	public List<BsbUser> getPageBsbUser(BsbUserExample bsbUserExample) {
		return bsbUserMapper.selectPage(bsbUserExample);
	}
	
	public int countBsbUser(BsbUserExample bsbUserExample) {
		return bsbUserMapper.countByExample(bsbUserExample);
	}
	
	public List<BsbUser> selectByExample(BsbUserExample example){
	    return bsbUserMapper.selectByExample(example);
	}
	/**
	 * 根据姓名模糊查询用户id集合
	 * @param name
	 * @return
	 */
	public List<String> getIdsLikeName(String name){
		if(StringUtils.isEmpty(name)){
			return Collections.EMPTY_LIST;
		}
		List<String> list = new ArrayList<String>();
		BsbUserExample example = new BsbUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameLike("%"+name+"%");
		
		List<BsbUser> users = bsbUserMapper.selectByExample(example  );
		for (BsbUser bsbUser : users) {
			String uuid = bsbUser.getUuid();
			list.add(uuid);
		}
		return list;
	}
	public int getCountUserFirm(String name,BsbUserExample example){
		
		return bsbUserExMapper.countUserFirmByExample(name, example);
		
	}
	
	public List<UserFirm> getUserFirms(String name,BsbUserExample example){
		return bsbUserExMapper.selectUserFirmByExample(name, example);
	}
	
	public UserFirm getUserFirmByUuid(String uuid){
		return bsbUserExMapper.selectUserFirmByUserId(uuid);
	}
}
